package com.linkhub.oms.admin.modules.product.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.linkhub.oms.admin.modules.order.domain.vo.ManualOrderImportResultVO;
import com.linkhub.oms.admin.modules.product.domain.param.ShopProductMappingParam;
import com.linkhub.oms.admin.modules.product.domain.param.ShopProductQueryParam;
import com.linkhub.oms.admin.modules.product.domain.vo.ShopProductImportResultVO;
import com.linkhub.oms.admin.modules.product.domain.vo.ShopProductVO;
import com.linkhub.oms.admin.modules.product.service.ProductMappingBizService;
import com.linkhub.oms.admin.modules.product.service.ShopProductBizService;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.common.response.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Description: ShopProductController
 * @Author: echo
 * @Date: 2024/3/20 11:09
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = {"商店商品管理模块"})
@RequestMapping("/shop_product")
@SaCheckLogin
@SaCheckRole(value = {RoleConstant.CUSTOMER})
public class ShopProductController {


    @Resource
    private ShopProductBizService shopProductBizService;
    @Resource
    private ProductMappingBizService productMappingBizService;

    @GetMapping("/list")
    @ApiOperation(value = "查询商店商品列表接口")
    public DataResponse<PageDTO<ShopProductVO>> listShopProducts(@Validated ShopProductQueryParam shopProductQueryParam) {
        return DataResponse.success(shopProductBizService.pageShopProducts(shopProductQueryParam));
    }

    @GetMapping("/list/{sysShopId}")
    @ApiOperation(value = "查询指定商店商品列表接口")
    public DataResponse<List<ShopProductVO>> listAssignShopProducts( @PathVariable("sysShopId") @Validated String sysShopId) {
        return DataResponse.success(shopProductBizService.listAssignShopProducts(sysShopId));
    }

    @PostMapping
    @ApiOperation(value = "更新所有商店商品接口")
    public DataResponse<Integer> syncShopProduct() {
        return DataResponse.success(shopProductBizService.manualUpdateShopProducts(LhSessionContext.getUserId()));
    }


    @PostMapping("/mapping")
    @ApiOperation(value = "匹配本地商品接口")
    public DataResponse<Void> saveMapping(@Valid @RequestBody ShopProductMappingParam shopProductMappingParam) {
        productMappingBizService.saveShopProductMapping(shopProductMappingParam);
        return DataResponse.success();
    }

    @PutMapping("/mapping")
    @ApiOperation(value = "更换匹配本地商品接口")
    public DataResponse<Void> updateMapping(@Valid @RequestBody ShopProductMappingParam shopProductMappingParam) {
        productMappingBizService.updateMapping(shopProductMappingParam);
        return DataResponse.success();
    }

//    @DeleteMapping("/mapping")
//    @ApiOperation(value = "解除匹配接口")
//    public DataResponse<Void> deleteMapping(@Valid @RequestBody ShopProductMappingParam shopProductMappingParam) {
//        productMappingBizService.deleteMapping(shopProductMappingParam);
//        return DataResponse.success();
//    }

    @PostMapping(value = "/import")
    @ApiOperation(value = "商店商品excel导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "商店商品excel文件", required = true,
                    dataType = "MultipartFile", dataTypeClass = MultipartFile.class, paramType = "form")
    })
    public DataResponse<ShopProductImportResultVO> importManualOrders( @RequestParam("file") MultipartFile shopProductExcel) {
        String userId = LhSessionContext.getUserId();
        return DataResponse.success(shopProductBizService.importManualOrders(userId, shopProductExcel));
    }

}
